<!--
 * @Author: your name
 * @Date: 2021-01-11 15:16:51
 * @LastEditTime: 2021-01-26 14:50:48
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\test\historyTestUpdata\historyTestUpdata.vue
-->
<template>
    <div class="test-historyTestUpdata-main-box">
        <div class="search-box">
            <div class="search-box-sub">
                <span class="lable-text">{{$t('devWifi.deviceSN')}}</span>
                <el-input v-big v-model="inverterSN" style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('salePage.wifiSN')}}</span>
                <el-input v-big v-model="wifiSN" style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('equipmentClassify')}}</span>
                <el-select v-model="type" filterable :placeholder="$t('please_choose')" clearable @change='typeChange'>
                    <el-option
                        v-for="item in dictCodes.DEVICE_CLASSIFY"
                        :key="item.value"
                        :label="item.code"
                        :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('version.versionAdd.compatible')}}</span>
                <el-select v-model="value" filterable :placeholder="$t('please_choose')" clearable @change='valueChange'>
                    <el-option
                        v-for="item in valueList"
                        :key="item.value"
                        :label="item.code"
                        :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('version.versionAdd.versions')}}</span>
                <el-select v-model="wifiVersion" filterable :placeholder="$t('please_choose')" clearable >
                    <el-option
                        v-for="item in versionList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <el-button type="primary" @click='query'>{{$t('query')}}</el-button>
            </div>
        </div>
        <div class="button-box">
            <el-button type="primary"  icon='el-icon-upload2' class="marginR10" @click='addMoadl'>{{$t('devWifi.batchUpgrade')}}</el-button>
        </div>
        <baseTable v-loading="loading"  row-key='sid' :data="dataRows" border  stripe :column='column' :select='true' @selection-change="handleSelectionChange($event)">
            <template #upgradeFormatter="{data}">
                <span>{{upgradeFormatter(data)}}</span>
            </template>
            <template #deviceType="{data}">
                <span>{{comm.returnCode(dictCodes.SCOPE_TO,data.deviceType)}}</span>
            </template>
            <template #inverterType="{data}">
                <span>{{comm.returnCode(dictCodes.INVERTER_TYPES,data.inverterType)}}</span>
            </template>
            <template #onlineFlag="{data}">
                <span :style='data.onlineFlag==1?"color:#429dff":""'>
                    <i class="iconfont icon-wifi" style="vertical-align: middle;" ></i>
                    {{data.onlineFlag == 1 ? $t('inventoryManage.statusLine'):$t('inventoryManage.statusOffline')}}
                </span>
            </template>
            <template #option="{data}">
                <div class="option-box">
                    <el-button style='font-size:18px;' type="danger" icon='el-icon-upload' :title="$t('devWifi.upgrade')" @click='showEditModal(data)'></el-button>
                    <el-button style='font-size:18px;'  v-if='data.updateFlag == 1' type="danger" icon='el-icon-refresh-left' :title="$t('reset.button')" @click='wifiEditEntity(data)'></el-button>
                </div>
            </template>
        </baseTable>
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
        <edit @uploadData='query' :editWifiSN='editWifiSN' :dictCodes='dictCodes'></edit>
        <batchImport @uploadData='query' :wifiSnArray='wifiSnArray' :armArry='armArry' :mkArry='mkArry' :dspArry='dspArry' :versionArray='versionArray' :inverterType='type' :scope='value'></batchImport>
    </div>
</template>

<script>
let setIntervalFun = ''
import baseTable from '../../../components/common/baseTable'
import edit from './edit'
import batchImport from './batchImport'
import { setInterval } from 'timers';
export default {
    components: {
        baseTable,
        edit,
        batchImport
    },
    props: {

    },
    data() {
        return {
            loading:false,
            inverterSN:'',
            wifiSN:'',
            type:'',
            value:'',
            current:1,
            size:10,
            total:1,
            wifiVersion:'',
            dictCodes:{},
            typeNameList:[],
            valueList:[],
            versionList:[],
            selectDataList:[],
            dataRows:[],
            column:[{
                title: this.$t('devWifi.deviceSN'),
                value: 'inverterSN',
                slot: 'inverterSN',
            },{
                title: this.$t('devWifi.wifiSN'),
                value: 'wifiSN',
                slot: 'wifiSN',
            },{
                title: this.$t('version.versionAdd.compatible'),
                value: 'deviceType',
                slot: 'deviceType',
            },{
                title: this.$t('devWifi.deviceType'),
                value: 'inverterType',
                slot: 'inverterType',
                min_width:'100'
            },{
                title: this.$t('devWifi.wifiVersion'),
                value: 'wifiVersion',
                slot: 'wifiVersion',
            },
            {
                title: this.$t('update.ARM'),
                value: 'armVersion',
                slot: 'armVersion',
            },
            {
                title: this.$t('update.DSP'),
                value: 'inverterVersion',
                slot: 'inverterVersion',
            },{
                title: this.$t('device.inverter.enableFlag'),
                value: 'onlineFlag',
                slot: 'onlineFlag',
            },{
                title: this.$t('devUpgrade.upgradeStatus'),
                value: 'wifiVersion',
                slot: 'upgradeFormatter',
            }, {
                title: this.$t('control'),
                value: 'sid',
                slot: 'option',
            }],
            editWifiSN:'',
            wifiSnArray:[],
            armArry:[],
            mkArry:[],
            dspArry:[],
            versionArray:[],
        };
    },
    computed: {

    },
    watch: {

    },
    created() {

    },
    mounted() {
        this.getDictionaryBatch()
    },
    methods: {
        async wifiEditEntity(row){
            let res = await this.API.deviceTestEditEntity({
                id:row.ids,
                updateFlag:0,
                updateStatusFlag:1
            })
            if(this.comm.showMsg(res.success)){
                this.query()
            }
        },
        showEditModal(row){
            if(row.onlineFlag==1){
                if(row.updateFlag==1){
                    this.comm.showMsg(false,this.$t('devUpgrade.waitForUpgrade'))
                }else{
                    this.editWifiSN = row.wifiSN
                    this.$store.commit('setVisibleEdit',true)
                }
            }else{
                this.comm.showMsg(false,this.$t('devUpgrade.EquipmentNotOnline'))
            }
            // this.$store.commit('setNewVisible',true)
        },
        upgradeFormatter(row){
            let updateStatusFlag=row.updateStatusFlag;
            let updateFlag=row.updateFlag;
            let progress=row.progress;
            let wifiSN=row.wifiSN;
            let str = ''
            if(updateFlag==1){
                if(progress==101){
                    str = `${this.$t('devUpgrade.download')} 0%`
                }else{
                    if(updateStatusFlag=='0'){
                        str = `${this.$t('devUpgrade.fialUpgrade')}`
                    }else if(updateStatusFlag==2){
                        if(progress&&progress!=this.$t('devUpgrade.downfinish')&&progress!=100){
                            str = `${this.$t('devUpgrade.download')} ${progress}%`
                        }else if(progress&&progress==this.$t('devUpgrade.downfinish')&&progress==100){
                            str = `${this.$t('devUpgrade.downfinish')}`
                        }
                    }else if(updateStatusFlag==1){
                        str = `${this.$t('version.versionAdd.versions')}${this.$t('version.versionAdd.succeed')}`
                    }else if(updateStatusFlag==null){
                        str = ' - '
                    }
                }
            }else if(updateFlag=='0'){
                if(updateStatusFlag==null){
                    str = ' - '
                }else{
                    str = `${this.$t('version.versionAdd.versions')}${this.$t('version.versionAdd.succeed')}`
                }
            }else if(updateFlag==null){
                str = ' - '
            }
            return str
        },
        async deviceTestGetUpgradePage(){
            this.loading = true
            let res = await this.API.deviceTestGetUpgradePage({
                provinceId: '',
                cityId: '',
                wifiSN: this.wifiSN,
                inverterSN: this.inverterSN,
                wifiVersion: this.wifiVersion == this.$t('all')?'':this.wifiVersion,
                type: this.type,
                value: this.value,
                size: this.size,
                current: this.current
            })
            this.loading = false
            this.dataRows = res.rows
            this.total = res.total
        },
        async addMoadl(){
            this.wifiSnArray=[];
            this.armArry=[];
            this.mkArry=[];
            this.dspArry=[];
            let selData = this.selectDataList
            let scope = this.value
            let inverterType = this.type
            let versions = this.versions
            for(var i=0; i<selData.length;i++){
                if(selData[i].onlineFlag!=1){//0和null   离线
                    this.comm.showMsg(false,this.$t('devUpgrade.unableuPata'))//选择项中含有离线设备，无法批量升级
                    //openMsg('选择项中含有离线设备，无法批量升级');
                    return;
                }
            }
            if(!scope || scope== this.$t('all') || !inverterType){
			    this.comm.showMsg(false,this.$t('devUpgrade.pleaseChooseFirst'))
			    return;
            }
            if(selData!=null && selData.length>0){
                if(inverterType==1||inverterType==3){
                    for (var i = 0; i < selData.length; i++) {// jsaonstring转object获取ids
                        if(selData[i].inverterType!=scope){
                            this.comm.showMsg(false,this.$t('devUpgrade.pleaseSearch'))//您勾选项的设备类型与要升级的设备类型不符，请重新勾选或查询
                            return false;
                        }
                    }
                }
                if(inverterType==2){
                    for (var i = 0; i < selData.length; i++) {// jsaonstring转object获取ids
                        if(selData[i].deviceType!=scope){
                            this.comm.showMsg(false,this.$t('devUpgrade.pleaseSearch'))//您勾选项的设备类型与要升级的设备类型不符，请重新勾选或查询
                            return false;
                        }
                    }
                }
                let ids='';//临时class值
                let flag=true;
                for (var i = 0; i < selData.length; i++) {// jsaonstring转object获取ids
                    this.wifiSnArray.push(selData[i].wifiSN);
                    if(ids.length==0){
                        ids+=selData[i].ids;
                    }else{
                        ids+=","+selData[i].ids;
                    }
                    if(selData[i].updateFlag==0||selData[i].updateFlag==null){
                        flag=false;
                    }
                    if(inverterType==1){
                        this.armArry.push(selData[i].armVersion);
                    }else if(inverterType==2){
                        this.mkArry.push(selData[i].wifiVersion);
                    }else if(inverterType==3){
                        this.dspArry.push(selData[i].inverterVersion);
                    }
                }
                if(ids!=''){
                    console.log(ids,'ids');
                    if(flag){
                        this.comm.showMsg(false,this.$t('devUpgrade.pleaseDelete'));//请至少选择一个未升级的设备
                    }else{
                        let data = await this.API.deviceTestGetVersionByType({
                            ids:ids,
                            type:inverterType,
                            value:scope,
                        })
                        if(data!=null && data.length>0){
                            this.versionArray = []
                            data.forEach(obj=>{
                                if(inverterType==1&&selData.length==1&&selData[0].armVersion==obj.version){
                                
                                }else if(inverterType==2&&selData.length==1&&selData[0].wifiVersion==obj.version){

                                }else if(inverterType==3&&selData.length==1&&selData[0].inverterVersion==obj.version){

                                }else{
                                    this.versionArray.push(obj);
                                }
                            })
                            if(versionArray.length==1){
                                this.comm.showMsg(false,this.$t('devUpgrade.noVersionToChoose'))//暂无可更新版本
                            }else{
                                openDialog("versionDialog", "views/test/historyBatchVersion.jsp");
                            }
                        }else{
                            this.comm.showMsg(false,this.$t('devUpgrade.noVersionToChoose'))
                        }
                    }
                }
            }else{
                this.comm.showMsg(false,this.$t('devUpgrade.pleaseChooseUpgrade'))
            }
        },
        query(){
            this.current = 1
            this.deviceTestGetUpgradePage()
        },
        typeChange(){
            if(this.type==1||this.type==3 || this.type==6){
                //机型
                this.valueList = this.dictCodes.INVERTER_TYPES
                this.versionList = []
            }else if(this.type==2){
                //wifi
                this.valueList = this.dictCodes.SCOPE_TO
                this.versionList = []
            }else {
                this.valueList = []
                this.versionList = []
            }
        },
        async valueChange(){
            if(this.value){
                let res = await this.API.deviceTestGetVersions({
                    type:this.type,
                    value:this.value,
                })
                if(res){
                    this.versionList = res
                }else{
                    this.versionList = []
                }
            }
        },
        async getDictionaryBatch(){
            let res = await this.API.getDictionaryBatch({
                dictCodes:`SN_STATUS,SCOPE_TO,INVERTER_TYPES,DEVICE_CLASSIFY,ON_LINE_STATE`,
                lang:localStorage.getItem('lang') || 'zh_CN'
            })
            this.dictCodes = res
            this.deviceTestGetUpgradePage()
            window.clearInterval(setIntervalFun);
            setIntervalFun = window.setInterval(()=>{
                this.deviceTestGetUpgradePage()
            },60*1000)
            
        },
        handleSizeChange(val) {
          this.size = val
          this.current = 1
          this.deviceTestGetUpgradePage()
        },
        handleCurrentChange(val) {
          this.current = val
          this.deviceTestGetUpgradePage()
        },
        handleSelectionChange(val){
          this.selectDataList=val
        },
    },
};
</script>

<style  lang="less" scoped>

</style>
